<template>
  <div>
    <el-dialog
      ref="table"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="dialogFormVisible"
      :append-to-body="true"
      title="维修项目选择"
      width="70%"
      @opened="searchHandle"
      @close="cancel" >
      <!-- 查询条件 -->
      <div class="searchform">
        <el-form
          ref="searchForm"
          :inline="true"
          :model="search"
          size="mini"
          label-width="100px"
          label-position="right"
        >
          <el-form-item
            prop="carTypeGroup "
            label="项目机型组"
          >
            <r-select
              v-model="search.carTypeGroup"
              :multiple="true"
              :clearable="true"
              class="search-input"
              selectid="MODEL_LABOUR_CODE"
              selectname="MODEL_LABOUR_NAME"
              url="/basedata/modelgroups/getMaintainModel/dicts"
              model="DMSCLOUD_REPAIR"
              @change="carTypeGroupchange"
            />
          </el-form-item>
          <el-form-item
            prop="catTypeCode"
            label="机型代码"
          >
            <el-input
              v-model="search.catTypeCode"
              placeholder="机型代码"
              class="search-input"
            />
            <el-input
              v-show="false"
              v-model="search.modeLabourCode"
            />
          </el-form-item>
          <el-form-item
            prop="primaryGroupName"
            label="主分类名称"
          >
            <r-select
              v-model="search.primaryGroupName"
              :clearable="true"
              :disabled="maindisabled"
              class="search-input"
              selectid="MAIN_GROUP_CODE"
              selectname="MAIN_GROUP_NAME"
              url="/basedata/labourgroups/getGroupList/dicts/items"
              model="DMSCLOUD_REPAIR"
            />
          </el-form-item>
          <el-form-item
            prop="subGroupName"
            label="二级分类名称"
          >
            <r-select
              v-model="search.subGroupName"
              :clearable="true"
              :disabled="subdisabled"
              :url="`/basedata/labourgroups/getGroupList/${primaryGroupName}`"
              class="search-input"
              selectid="SUB_GROUP_CODE"
              selectname="SUB_GROUP_NAME"
              model="DMSCLOUD_REPAIR"
            />
          </el-form-item>
          <el-form-item
            prop="repairCode"
            label="维修项目代码"
          >
            <el-input
              v-model="search.repairCode"
              placeholder="维修项目代码"
              class="search-input"
            />
          </el-form-item>
          <el-form-item
            prop="repairName"
            label="维修项目名称"
          >
            <el-input
              v-model="search.repairName"
              placeholder="维修项目名称"
              class="search-input"
            />
          </el-form-item>
          <el-form-item
            prop="repairSpell"
            label="维修项目拼音"
          >
            <el-input
              v-model="search.repairSpell"
              placeholder="维修项目拼音"
              class="search-input"
            />
          </el-form-item>

          <el-form-item
            prop="dataSources"
            label="资料来源"
          >
            <r-select
              v-model="search.dataSources"
              :clearable="true"
              type="1045"
              class="search-input"
            />
          </el-form-item>
          <el-form-item
            prop="localCode"
            label="行管项目代码"
          >
            <el-input
              v-model="search.localCode"
              placeholder="行管项目代码"
              class="search-input"
            />
          </el-form-item>
          <el-form-item
            prop="localName"
            label="行管项目名称"
          >
            <el-input
              v-model="search.localName"
              placeholder="行管项目名称"
              class="search-input"
            />
          </el-form-item>
          <div style="text-align:center">
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="searchHandle"
              >查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button
                icon="el-icon-refresh"
                @click="resetForm('searchForm')"
              >重置</el-button>
            </el-form-item>
          </div>
        </el-form>
      </div>
      <div class="table">
        <!-- 维修项目表格 -->
        <div class="table">
          <div class="tableHead">
            <div class="tabletitle">维修项目</div>
          </div>
          <BaseTable
            ref="repairtable"
            @chaneg-size="changeSizeHandle"
            @chaneg-page="changePageHandle"
            @row-dblclick="dblclick"
          >
            <el-table-column
              label="序号"
              type="index"
            />
            <el-table-column
              align="center"
              label="操作"
              min-width="100px"
            >
              <template slot-scope="scope">
                <el-radio v-model="radio" :label="scope.$index" @change.native="repairSelect(scope.$index,scope.row)" >&nbsp;</el-radio>
              </template>
            </el-table-column>
            <el-table-column
              prop="LABOUR_CODE"
              label="维修项目代码"
              width="150"
            />
            <el-table-column
              prop="LABOUR_NAME"
              label="维修项目名称"
              width="150"
            />
            <el-table-column
              :formatter="filterCode"
              prop="REPAIR_TYPE_CODE"
              label="维修分类"
              width="150"
              type="1220"
            />

            <el-table-column
              prop="STD_LABOUR_HOUR"
              label="标准工时"
              width="150"
            />
            <el-table-column
              prop="ASSIGN_LABOUR_HOUR"
              label="派工工时"
              width="150"
            />
            <el-table-column
              prop="CLAIM_LABOUR"
              label="索赔工时"
              width="150"
            />

            <el-table-column
              prop="MAIN_GROUP_CODE"
              label="主分类代码"
              width="150"
            />
            <el-table-column
              prop="SUB_GROUP_CODE"
              label="二级分类代码"
              width="150"
            />
            <!--              <el-table-column
                prop="IS_CHANGEWO"
                label="名称工单可改"
                width="150"
                type="1004"
                :formatter="filterCode"
              ></el-table-column>

              <el-table-column
                prop="LOCAL_LABOUR_CODE"
                label="行管项目代码"
                width="150"
              ></el-table-column>
              <el-table-column
                prop="LOCAL_LABOUR_NAME"
                label="行管项目名称"
                width="150"
              ></el-table-column> -->
            <el-table-column
              :formatter="filterCode"
              prop="DATA_SOURCES"
              label="数据来源"
              width="150"
              type="1045"
            />
          </BaseTable>

        </div>
        <!-- 配件表格 -->
        <div class="table">
          <div class="tableHead">
            <div class="tabletitle">配件</div>
          </div>
          <el-table
            ref="parttable"
            :data="partTableData"
            @chaneg-size="changeSizeHandlePart"
            @chaneg-page="changePageHandlePart"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              label="序号"
              type="index"
            />
            <el-table-column
              type="selection"
              width="55"/>
            <el-table-column
              prop="STORAGE_NAME"
              label="仓库名称"
              width="150"
            />
            <el-table-column
              prop="PART_NO"
              label="配件代码"
              width="150"
            />
            <el-table-column
              prop="PART_NAME"
              label="配件名称"
              width="150"
            />
            <el-table-column
              prop="PART_QUAUTITY_NUM"
              label="数量"
              width="150"
            />
            <el-table-column
              prop="AVAILABLE"
              label="可用库存"
              width="150"
            />
            <el-table-column
              prop="PART_SALES_PRICE"
              label="销售价"
              width="150"
            />
            <el-table-column
              prop="LABOUR_CODE"
              label="维修项目代码"
              width="150"
            />
            <!-- <el-table-column
                prop="MODEL_LABOUR_CODE"
                label="维修机型组代码"
                width="150"
              ></el-table-column> -->
            <el-table-column
              :formatter="filterCode"
              prop="PART_STATUS"
              label="是否停用"
              width="150"
              type="1004"
            />
          </el-table>
          <div class="center">
            <Pagination
              :data="PartPage"
              @change-size="handleSizeChangePart"
              @change-page="handleCurrentChangePart"
            />
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer" style="text-align:center" >
        <!-- <el-button type="text"><i class="el-icon-search magnifier"></i></el-button> -->
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="resetProject" >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryRepairPros, queryPartsByLabour, getCarType
} from '@/api/repair/basedata/repairmanager/repairmanager'
import BaseTable from '@/components/BaseTable'
import rSelect from '@/components/Select/Select'
import formMixins from '@/mixins/formMixins'
import selectPartInfo from './selectPartInfo'
import Pagination from '@/components/Pagination'

export default {
  name: 'Labourgroup',
  components: {
    BaseTable,
    rSelect,
    Pagination,
    selectPartInfo
  },
  mixins: [formMixins],
  props: {
    dataItem: Array
  },
  data() {
    return {
      dialogFormVisible: true,
      multipleSelection: [],
      parentmultipleSelection: [],
      radio: '',
      search: {
        limit: 10,
        pageNum: 1,
        carTypeGroup: '',
        modeLabourCode: '',
        primaryGroupName: '',
        subGroupName: '',
        repairCode: '',
        repairName: '',
        repairSpell: '',
        dataSources: '',
        localCode: '',
        localName: '',
        groupCode: '',
        subGroupCode: ''
      },
      maindisabled: false,
      subdisabled: true,
      loading: false,
      maindialogFormVisible: false,
      searchPart: {
        limit: 10,
        pageNum: 1
      },
      partTableData: [],
      PartPage: {}
    }
  },
  computed: {
    primaryGroupName: function() {
      this.search.subGroupName = ''
      if (this.search.primaryGroupName === '') {
        this.subdisabled = true
      } else {
        (
          this.subdisabled = false
        )
      }
      return this.search.primaryGroupName
    },
    main_group_code: function() {
      if (this.repairform.main_group_code === '') {
        this.repairform.sub_group_code = ''
      }
      return this.repairform.main_group_code
    }
  },
  updated() {
    this.searchHandle()
  },

  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    resetProject() {
      var repairPro = this.parentmultipleSelection
      var isDouble = false
      var lastRepairPro = this.$props.dataItem
      if (lastRepairPro.length > 0) {
        var currentLabour = repairPro.LABOUR_CODE + '|' + repairPro.MODEL_LABOUR_CODE
        for (var i = 0; i < lastRepairPro.length; i++) {
          var lastLabour = lastRepairPro[i].LABOUR_CODE + '|' + lastRepairPro[i].MODEL_LABOUR_CODE
          if (lastLabour === currentLabour) {
            isDouble = true
          }
        }
      }
      if (isDouble) {
        this.$message({
          type: 'info',
          message: '重复添加'
        })
      } else {
        this.confirmHandle()
      }
    },
    confirmHandle() {
      if (this.parentmultipleSelection.length !== 0) {
        const orderNum = this.$props.dataItem.length
        this.parentmultipleSelection.labourCode = this.parentmultipleSelection.LABOUR_CODE
        this.parentmultipleSelection.labourName = this.parentmultipleSelection.LABOUR_NAME
        this.$set(this.parentmultipleSelection, 'assignLabourHour', this.parentmultipleSelection.ASSIGN_LABOUR_HOUR)
        this.$set(this.parentmultipleSelection, 'stdLabourHour', this.parentmultipleSelection.STD_LABOUR_HOUR)
        this.$set(this.parentmultipleSelection, 'modelLabourCode', this.parentmultipleSelection.MODEL_LABOUR_CODE)
        this.$set(this.parentmultipleSelection, 'discount', 1)
        this.$set(this.parentmultipleSelection, 'chargePartitionCode', '')
        this.$set(this.parentmultipleSelection, 'orderNum', orderNum + 1)
        this.$emit('Labour', this.parentmultipleSelection)
        if (this.multipleSelection.length !== 0) {
          this.multipleSelection.forEach(el => {
            el.available = el.AVAILABLE
            el.orderNum = orderNum + 1
            el.costAmount = el.COST_AMOUNT
            el.costPrice = el.COST_PRICE
            el.dealerCode = el.DEALER_CODE
            el.labourCode = el.LABOUR_CODE
            el.labourName = this.parentmultipleSelection.labourName
            el.labourPartId = el.LABOUR_PART_ID
            el.modelLabourCode = el.MODEL_LABOUR_CODE
            el.modelLabourName = el.MODEL_LABOUR_NAME
            el.partName = el.PART_NAME
            el.partNo = el.PART_NO
            this.$set(el, 'partQuantity', el.PART_QUAUTITY_NUM)
            this.$set(el, 'partSalesPrice', el.PART_SALES_PRICE)
            el.partStatus = el.PART_STATUS
            el.partStockId = el.PART_STOCK_ID
            el.stockQuantity = el.STOCK_QUANTITY
            el.storageCode = el.STORAGE_CODE
            el.storageName = el.STORAGE_NAME
            el.storagePositionCode = el.STORAGE_POSITION_CODE
            el.unit = el.UNIT
            el.discount = 1
          })
          this.$message({
            type: 'success',
            message: '添加成功'
          })
          this.$emit('LaPart', this.multipleSelection)
        }
      } else {
        this.$message({
          type: 'warning',
          message: '请选择表格数据!'
        })
      }
    },
    // 改变当前也显示数目
    changeSizeHandlePart(table, size) {
      this.searchPart.limit = size
      // this.subgroups(this.labourdata);
    },
    // 改变页数
    changePageHandlePart(table, num) {
      this.searchPart.pageNum = num
      // this.subgroups(this.labourdata);
    },
    handleSizeChangePart(size) {
      this.$emit('chaneg-size', this, size)
    },
    handleCurrentChangePart(current) {
      this.$emit('chaneg-page', this, current)
    },
    // 获取维修项目信息
    searchHandle() {
      const table = this.$refs['repairtable']
      queryRepairPros(this.search)
        .then(res => {
          table.complete().filData(res)
        })
        .catch(err => {
          // table.complete();
        })
    },
    // 项目机型组绑定变更事件
    carTypeGroupchange() {
      var carTypeGroData = this.search.carTypeGroup
      if (carTypeGroData.length > 0) {
        var modeLabourCode = carTypeGroData.join('-')
        var modeLabourCodeSearch = carTypeGroData.join(',')
        // $("input[name='modeLabourCode']").val(modeLabourCodeSearch);
        this.search.modeLabourCode = modeLabourCodeSearch
        getCarType(modeLabourCode)
          .then(modelData => {
            var sameType // 同类型的
            var defType = []// 不同类型的
            for (var i = 0; i < carTypeGroData.length; i++) {
              var modecodes = modelData[carTypeGroData[i]]
              var tempData
              sameType = []// 每次去掉重复的
              modecodes.each(function(i, data) {
                sameType.push(data.MODEL_CODE)
              })
              tempData = sameType.join(',')
              defType.push(tempData)
            }
            this.search.catTypeCode = defType.join(';')
            // $("input[name='catTypeCode']",container).val(defType.join(";"));
          })
          .catch(err => {
          })
      } else {
        this.search.catTypeCode = ''
        this.search.modeLabourCode = ''
      }
    },
    // 双击维修项目选择
    dblclick(row) {
      this.parentmultipleSelection = row
      queryPartsByLabour(row.LABOUR_ID, this.searchPart)
        .then(res => {
          this.partTableData = res.rows
          this.PartPage = res
        })
        .catch(err => {
          // table.complete();
        })
    },
    // 维修项目选择,双击
    repairSelect(index, data) {
      this.dblclick(data)
    },
    cancel() {
      this.$emit('cancel1', 1)
      this.dialogFormVisible = false
    }
  }
}
</script>

<style scoped>
</style>
